Enegizing change

Introduction

RQ1

df_seasonality <- read.csv("../data/df_seasonality.csv")
map_data_sf <- readRDS("../data/map_data_sf.rds")

Car Registrations Over Time


Trend over Time

# Ensure the YearMonth is in Date format
df_seasonality$YearMonth <- as.Date(df_seasonality$YearMonth, format = "%Y-%m-%d")

# Calculate a smoothed series using a rolling mean or LOESS
# For a rolling mean:
df_seasonality$Smoothed <- rollmean(df_seasonality$Count, k = 12, fill = NA)

# Alternatively, for LOESS smoothing:
# loess_fit <- loess(Count ~ as.numeric(YearMonth), data = df_seasonality, span = 0.5)
# df_seasonality$Smoothed <- predict(loess_fit)

# Create an xts object with both the original and smoothed counts
df_xts <- xts(df_seasonality[, c("Count", "Smoothed")], order.by = df_seasonality$YearMonth)

# Plot using dygraphs
dygraph_object <- dygraph(df_xts, main = "Passenger Car Adoption Over Time in Switzerland") %>%
  dySeries("Count", label = "Number of Passenger Cars Registered") %>%
  dySeries("Smoothed", label = "Smoothed Trend") %>%
  dyOptions(stackedGraph = FALSE) %>%
  dyRangeSelector(height = 20)

# Print the dygraph to display it
dygraph_object

Monthly Trend

p_seaso_2 <- ggplot(df_seasonality, aes(x = Month, y = Count, group = Year, color = as.factor(Year))) +
  geom_smooth(se = FALSE, method = "loess", span = 0.5, size = 0.7) +
  labs(title = "Monthly Passenger Car Registrations by Year",
       x = "Month",
       y = "Number of Passenger Cars Registered",
       color = "Year") +
  theme_minimal() +
  scale_color_viridis_d() +
  theme(legend.position = "bottom", axis.text.x = element_text(angle = 45, hjust = 1))
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
# Convert to an interactive plotly object
interactive_plot_seaso_2 <- ggplotly(p_seaso_2)
`geom_smooth()` using formula = 'y ~ x'
# Adjust plotly settings if needed, such as margins or layout
interactive_plot_seaso_2 <- interactive_plot_seaso_2 %>%
  layout(margin = list(l = 10, r = 10, b = 40, t = 40), # Adjust margins
         legend = list(orientation = "h", x = 0, xanchor = "left", y = -0.2)) # Adjust legend position

# Display the interactive plot
(interactive_plot_seaso_2)

Monthly Trend 2

df_seasonality$Year <- as.factor(df_seasonality$Year)
p_seaso_3 <- ggplot(df_seasonality, aes(x = Month, y = Count, group = Year, color = Year)) +
  geom_line() +
  facet_wrap(~ Year, scales = "free_y") +
  labs(title = "Seasonal Trends in Passenger Car Registrations",
       x = "Month",
       y = "Number of Passenger Cars Registered") +
  theme_minimal() +
  scale_color_viridis_d(guide = FALSE) +
  theme(axis.text.x = element_blank(),  # Combine axis.text.x settings here
        axis.text.y = element_blank(),
        axis.ticks.x = element_blank(),
        legend.position = "none")

interactive_plot_seaso_3 <- ggplotly(p_seaso_3) %>%
  layout(xaxis = list(tickmode = "array",
                      tickvals = 1:12,
                      ticktext = month.abb))

interactive_plot_seaso_3

map

detach("package:xts", unload = TRUE)
# Create color palettes for the 'Total' and 'EV_per_Capita' columns
color_palette_total <- colorNumeric(palette = "viridis", domain = map_data_sf$TotalEV)

leaflet(map_data_sf) %>%
    addProviderTiles(providers$CartoDB.Positron) %>%
    addPolygons(
      fillColor = ~color_palette_total(TotalEV),
      weight = 1,
      color = "#FFFFFF",
      fillOpacity = 0.7,
      popup = ~paste(NAME, "<br>Total EV Registrations: ", TotalEV)
    ) %>%
    addLegend(
      pal = color_palette_total, 
      values = ~TotalEV, 
      opacity = 0.7, 
      title = "Total EV <br> Registrations",
      position = "topright"
    )

map2

Column

Car Details

# Assuming you have a data frame named df_seasonality
datatable(df_seasonality[, !names(df_seasonality) %in% c("Smoothed", "YearMonth")], options = list(pageLength = 10, autoWidth = TRUE))